/*
 * Copyright 2025 Daytona Platforms Inc.
 * SPDX-License-Identifier: Apache-2.0
 */

import { SandboxCodeToolbox } from '../Sandbox'
import { CodeRunParams } from '../Process'
import { Buffer } from 'buffer'

export class SandboxPythonCodeToolbox implements SandboxCodeToolbox {
  public getRunCommand(code: string, params?: CodeRunParams): string {
    // Encode the provided code in base64
    let base64Code = Buffer.from(code).toString('base64')

    // Override plt.show() method if matplotlib is imported
    if (SandboxPythonCodeToolbox.isMatplotlibImported(code)) {
      let code_wrapper = Buffer.from(PYTHON_CODE_WRAPPER, 'base64').toString('utf-8')
      code_wrapper = code_wrapper.replace('{encoded_code}', base64Code)
      base64Code = Buffer.from(code_wrapper).toString('base64')
    }

    // Build command-line arguments string
    const argv = params?.argv ? params.argv.join(' ') : ''

    // Execute the bootstrapper code directly
    // Use -u flag to ensure unbuffered output for real-time error reporting
    // eslint-disable-next-line no-useless-escape
    return `sh -c 'python3 -u -c "exec(__import__(\\\"base64\\\").b64decode(\\\"${base64Code}\\\").decode())" ${argv}'`
  }

  /**
   * Checks if matplotlib is imported in the given Python code string.
   * @param codeString Python code as a string
   * @returns True if matplotlib is imported, false otherwise
   */
  private static isMatplotlibImported(codeString: string): boolean {
    // Regex patterns for different import styles
    const patterns: RegExp[] = [
      // Standard imports
      /^[^#]*import\s+matplotlib/m,
      /^[^#]*from\s+matplotlib/m,

      // Dynamic imports
      /^[^#]*__import__\s*\(\s*['"]matplotlib['"]/m,
      /^[^#]*importlib\.import_module\s*\(\s*['"]matplotlib['"]/m,

      // Other dynamic loading patterns
      /^[^#]*loader\.load_module\s*\(\s*['"]matplotlib['"]/m,
      /^[^#]*sys\.modules\[['"]matplotlib['"]\]/m,
    ]

    // Check each pattern
    for (const pattern of patterns) {
      if (pattern.test(codeString)) {
        return true
      }
    }

    return false
  }
}

const PYTHON_CODE_WRAPPER = `
aW1wb3J0IGJhc2U2NAppbXBvcnQgZGF0ZXRpbWUKaW1wb3J0IGhhc2hsaWIKaW1wb3J0IGlvC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0aWNrbGFiZWxzID0gW2xhYmVsLmdldF90ZXh0KCkgZm9yIGxhYmVsIGluIGF4LmdldF94
dGlja2xhYmVscygpXQogICAgYm94ZXMgPSBbXQogICAgZm9yIGxhYmVsLCBib3ggaW4gemlwKHh0
aWNrbGFiZWxzLCBheC5wYXRjaGVzKToKICAgICAgICB2ZXJ0aWNlcyA9IGJveC5nZXRfcGF0aCgp
LnZlcnRpY2VzCiAgICAgICAgeF92ZXJ0aWNlcyA9IGxpc3QodmVydGljZXNbOiwgMF0pCiAgICAg
ICAgeV92ZXJ0aWNlcyA9IGxpc3QodmVydGljZXNbOiwgMV0pCiAgICAgICAgeCA9IG1pbih4X3Zl
cnRpY2VzKQogICAgICAgIHkgPSBtaW4oeV92ZXJ0aWNlcykKCiAgICAgICAgYm94ZXMuYXBwZW5k
KAogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAieCI6IHgsCiAgICAgICAgICAgICAgICAi
eSI6IHksCiAgICAgICAgICAgICAgICAibGFiZWwiOiBsYWJlbCwKICAgICAgICAgICAgICAgICJ3
aWR0aCI6IG1heCh4X3ZlcnRpY2VzKSAtIHgsCiAgICAgICAgICAgICAgICAiaGVpZ2h0IjogbWF4
KHlfdmVydGljZXMpIC0geSwKICAgICAgICAgICAgICAgICJvdXRsaWVycyI6IFtdLAogICAgICAg
ICAgICB9CiAgICAgICAgKQoKICAgIG9yaWVudGF0aW9uID0gImhvcml6b250YWwiCiAgICBpZiBh
bGwoYm94WyJoZWlnaHQiXSA9PSBib3hlc1swXVsiaGVpZ2h0Il0gZm9yIGJveCBpbiBib3hlcyk6
CiAgICAgICAgb3JpZW50YXRpb24gPSAidmVydGljYWwiCgogICAgaWYgb3JpZW50YXRpb24gPT0g
InZlcnRpY2FsIjoKICAgICAgICBjaGFuZ2Vfb3JpZW50YXRpb24gPSBUcnVlCiAgICAgICAgZm9y
IGJveCBpbiBib3hlczoKICAgICAgICAgICAgYm94WyJ4Il0sIGJveFsieSJdID0gYm94WyJ5Il0s
IGJveFsieCJdCiAgICAgICAgICAgIGJveFsid2lkdGgiXSwgYm94WyJoZWlnaHQiXSA9IGJveFsi
aGVpZ2h0Il0sIGJveFsid2lkdGgiXQoKICAgIGZvciBsaW5lIGluIGF4LmxpbmVzOgogICAgICAg
IHhkYXRhID0gbGluZS5nZXRfeGRhdGEoKQogICAgICAgIHlkYXRhID0gbGluZS5nZXRfeWRhdGEo
KQoKICAgICAgICBpZiBvcmllbnRhdGlvbiA9PSAidmVydGljYWwiOgogICAgICAgICAgICB4ZGF0
YSwgeWRhdGEgPSB5ZGF0YSwgeGRhdGEKCiAgICAgICAgaWYgbGVuKHhkYXRhKSA8PSAxIG9yIGxl
bih5ZGF0YSkgIT0gMjoKICAgICAgICAgICAgY29udGludWUKCiAgICAgICAgZm9yIGJveCBpbiBi
b3hlczoKICAgICAgICAgICAgaWYgYm94WyJ4Il0gPD0geGRhdGFbMF0gPD0geGRhdGFbMV0gPD0g
Ym94WyJ4Il0gKyBib3hbIndpZHRoIl06CiAgICAgICAgICAgICAgICAjIEhvcml6b250YWwgbGlu
ZSAobWVkaWFuIG9yIGNhcCkKICAgICAgICAgICAgICAgIGlmIGFicyh5ZGF0YVswXSAtIHlkYXRh
WzFdKSA8IDAuMDAxIGFuZCBib3hbInkiXSA8PSB5ZGF0YVswXSA8PSBib3hbInkiXSArIGJveFsi
aGVpZ2h0Il06CiAgICAgICAgICAgICAgICAgICAgYm94WyJtZWRpYW4iXSA9IHlkYXRhWzBdCiAg
ICAgICAgICAgICAgICAjIFZlcnRpY2FsIGxpbmUgKHdoaXNrZXJzKQogICAgICAgICAgICAgICAg
ZWxpZiBhYnMoeGRhdGFbMF0gLSB4ZGF0YVsxXSkgPCAwLjAwMToKICAgICAgICAgICAgICAgICAg
ICB5X21pbiA9IG1pbih5ZGF0YSkKICAgICAgICAgICAgICAgICAgICB5X21heCA9IG1heCh5ZGF0
YSkKCiAgICAgICAgICAgICAgICAgICAgIyBJZiBhdHRhY2hlZCB0byBib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pbiByYW5nZShtaW4obGVuKHhkYXRhKSwgbGVuKHlkYXRhKSkpOgogICAg
ICAgICAgICAgICAgICAgICAgICBvdXRsaWVyX2NhbmRpZGF0ZXMuYXBwZW5kKChmbG9hdCh4ZGF0
YVtpXSksIGZsb2F0KHlkYXRhW2ldKSkpCiAgICAgICAgICAgIGV4Y2VwdDoKICAgICAgICAgICAg
ICAgIHBhc3MKCiAgICAjIEFzc2lnbiBwb2ludHMgdG8gYm94ZXMgYW5kIGRldGVybWluZSBpZiB0
aGV5J3JlIG91dGxpZXJzCiAgICBmb3IgeCwgeSBpbiBvdXRsaWVyX2NhbmRpZGF0ZXM6CiAgICAg
ICAgZm9yIGJveCBpbiBib3hlczoKICAgICAgICAgICAgaWYgYm94WyJ4Il0gPD0geCA8PSBib3hb
IngiXSArIGJveFsid2lkdGgiXToKICAgICAgICAgICAgICAgIGJveF9jZW50ZXIgPSBib3hbIngi
XSArIGJveFsid2lkdGgiXSAvIDIKICAgICAgICAgICAgICAgIGlmIGFicyh4IC0gYm94X2NlbnRl
cikgPCAwLjAwMToKICAgICAgICAgICAgICAgICAgICB5X21pbiA9IGJveFsieSJdCiAgICAgICAg
ICAgICAgICAgICAgeV9tYXggPSBib3hbInkiXSArIGJveFsiaGVpZ2h0Il0KICAgICAgICAgICAg
ICAgICAgICBpZiBib3guZ2V0KCJ3aGlza2VyX2xvd2VyIiwgTm9uZSk6CiAgICAgICAgICAgICAg
ICAgICAgICAgIHlfbWluID0gYm94WyJ3aGlza2VyX2xvd2VyIl0KICAgICAgICAgICAgICAgICAg
ICBpZiBib3guZ2V0KCJ3aGlza2VyX3VwcGVyIiwgTm9uZSk6CiAgICAgICAgICAgICAgICAgICAg
ICAgIHlfbWF4ID0gYm94WyJ3aGlza2VyX3VwcGVyIl0KICAgICAgICAgICAgICAgICAgICBpZiB5
IDwgeV9taW4gb3IgeSA+IHlfbWF4OgogICAgICAgICAgICAgICAgICAgICAgICBib3hbIm91dGxp
ZXJzIl0uYXBwZW5kKHkpCiAgICAgICAgICAgICAgICBicmVhawoKICAgIHJldHVybiBbCiAgICAg
ICAgewogICAgICAgICAgICAibGFiZWwiOiBib3hbImxhYmVsIl0sCiAgICAgICAgICAgICJtaW4i
OiBib3guZ2V0KCJ3aGlza2VyX2xvd2VyIiwgTm9uZSksCiAgICAgICAgICAgICJmaXJzdF9xdWFy
dGlsZSI6IGJveFsieSJdLAogICAgICAgICAgICAibWVkaWFuIjogYm94LmdldCgibWVkaWFuIiwg
Tm9uZSksCiAgICAgICAgICAgICJ0aGlyZF9xdWFydGlsZSI6IGJveFsieSJdICsgYm94WyJoZWln
aHQiXSwKICAgICAgICAgICAgIm1heCI6IGJveC5nZXQoIndoaXNrZXJfdXBwZXIiLCBOb25lKSwK
ICAgICAgICAgICAgIm91dGxpZXJzIjogYm94WyJvdXRsaWVycyJdLAogICAgICAgIH0KICAgICAg
ICBmb3IgYm94IGluIGJveGVzCiAgICBdLCBjaGFuZ2Vfb3JpZW50YXRpb24KCgpkZWYgX3NhdmVf
ZmlndXJlX2FzX2Jhc2U2NChmaWcsIGJib3hfaW5jaGVzPSJ0aWdodCIsIGRwaT0xMDApOgogICAg
IyBGaXJzdCBzYXZlIHdpdGggbWF0cGxvdGxpYgogICAgcG5nX2J1ZmZlciA9IGlvLkJ5dGVzSU8o
KQogICAgZmlnLnNhdmVmaWcocG5nX2J1ZmZlciwgZm9ybWF0PSJwbmciLCBiYm94X2luY2hlcz1i
Ym94X2luY2hlcywgZHBpPWRwaSkKICAgIHBuZ19idWZmZXIuc2VlaygwKQoKICAgICMgT3BlbiB3
aXRoIFBJTCBhbmQgYXBwbHkgbWF4aW11bSBjb21wcmVzc2lvbgogICAgd2l0aCBwaWxfaW1nLm9w
ZW4ocG5nX2J1ZmZlcikgYXMgaW1nOgogICAgICAgIG9wdGltaXplZF9idWZmZXIgPSBpby5CeXRl
c0lPKCkKICAgICAgICBpbWcuc2F2ZShvcHRpbWl6ZWRfYnVmZmVyLCBmb3JtYXQ9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